/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.vlivetest20140310;

import com.elcom.emocbc.CbmTool;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author GiangLT
 */
public class CaptureFileTimeline {

    public static void main(String[] args) {
        CaptureFileTimeline captureFileTimeline = new CaptureFileTimeline();
        captureFileTimeline.doProcess();

    }

    public void doProcess() {
        //        PropertyConfigurator.configureAndWatch(CONFIG_FOLDER + MODULE_NAME + "-log4j.props");
//        final String CBM_FOLDER = "F:\\Projects\\Livescreen\\dump-analysis\\BacNinh20130324\\cbm3\\";
        final String CBM_FOLDER = "F:\\Projects\\Livescreen\\testing\\test-2014-05-20\\CBM\\";
        File folder = new File(CBM_FOLDER);
        File[] cbmFiles = folder.listFiles();
//        Arrays.sort(cbmFiles, new Comparator<File>() {
//            @Override
//            public int compare(File o1, File o2) {
//                return o1.getName().compareTo(o2.getName());
//            }
//        });

        List<Timeslot> timeslots = Lists.newArrayListWithCapacity(cbmFiles.length);
        for (File file : cbmFiles) {
            if (file.isFile()) {
                try {
                    byte[] cbmArray = IOUtils.toByteArray(new FileInputStream(file));
                    byte[] normalizedCbmArray = Arrays.copyOfRange(cbmArray, 6, cbmArray.length);
                    byte msgId = normalizedCbmArray[0];
                    byte pageIndicator = normalizedCbmArray[1];
                    Date date = parseFileNameToDate(file.getName());

                    Timeslot timeslot = new Timeslot(date, msgId, pageIndicator, normalizedCbmArray);
                    timeslots.add(timeslot);
//                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.S");
//                    System.out.println(String.format("File: %s \t- MsgId %s - PageId %s", df.format(date), CbmTool.toHexString(msgId), CbmTool.toHexString(pageIndicator)));
                } catch (IOException ex) {
                    Logger.getLogger(CaptureFileTimeline.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        Collections.sort(timeslots);
        for (Timeslot timeslot : timeslots) {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            System.out.println(String.format("File: %s\t%s\t%s\t%s", df.format(timeslot.getDatetime()), CbmTool.toHexString(timeslot.getMsgId()), CbmTool.toHexString(timeslot.getPageId()), CbmTool.toHexString(timeslot.getData())));
        }
    }

    private Date parseFileNameToDate(String fileName) {
        String[] tokens = fileName.split("_");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, Integer.parseInt(tokens[1]) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tokens[2]));
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens[3]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(tokens[4]));
        calendar.set(Calendar.SECOND, Integer.parseInt(tokens[5]));

//        String milisec = tokens[6].substring(0, tokens[6].lastIndexOf(" .txt"));
//        calendar.set(Calendar.MILLISECOND, Integer.parseInt(milisec));

        return calendar.getTime();
    }

    private class Timeslot implements Comparable<Timeslot> {

        private Date datetime;
        private byte msgId;
        private byte pageId;
        private byte[] data;

        public Timeslot() {
        }

        public Timeslot(Date datetime, byte msgId, byte pageId, byte[] data) {
            this.datetime = datetime;
            this.msgId = msgId;
            this.pageId = pageId;
            this.data = data;
        }

        public byte[] getData() {
            return data;
        }

        public void setData(byte[] data) {
            this.data = data;
        }

        public Date getDatetime() {
            return datetime;
        }

        public void setDatetime(Date datetime) {
            this.datetime = datetime;
        }

        public byte getMsgId() {
            return msgId;
        }

        public void setMsgId(byte msgId) {
            this.msgId = msgId;
        }

        public byte getPageId() {
            return pageId;
        }

        public void setPageId(byte pageId) {
            this.pageId = pageId;
        }

        @Override
        public int compareTo(Timeslot o) {
            return this.datetime.compareTo(o.getDatetime());
        }

    }
}
